#include "Game.h"
#define _CRT_SECURE_NO_WARNINGS

LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
bool    initWindow(HINSTANCE hInstance);
HWND wndHandle;					// global window handle

/**********************************************************************
* WinMain - entry point
**********************************************************************/
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
   Game game(640,480);
   MSG msg;                                           // Message object.                                         // Handle to the window.
   // Initialize Direct3D.  If fails then we don't want to run the application.
   	if (!initWindow(hInstance))
	{
		MessageBox(NULL, "Unable to create window", "ERROR", MB_OK);
		return false;
	}

   	if (!dxMgr.initDirect3D(wndHandle, 0))
	{
		MessageBox(NULL, "Unable to init Direct3D", "ERROR", MB_OK);
		return false;
	}

	// initialize DirectSound
	if (!dxAmgr.initDirectSound(wndHandle))
	{
		MessageBox(NULL, "Unable to init DirectSound", "ERROR", MB_OK);
		return false;
	}

	diMgr.initDirectInput(hInstance, wndHandle);
	ZeroMemory( &msg, sizeof(msg) );
	msg = game.gameLoop();
    // Here we unregister the window class with the OS.
    UnregisterClass("Novablaze", hInstance);

   return (int)msg.wParam;
}

bool initWindow(HINSTANCE hInstance)
{
	WNDCLASSEX wcex;

	wcex.cbSize = sizeof(WNDCLASSEX);
	wcex.style			= CS_HREDRAW | CS_VREDRAW;
	wcex.lpfnWndProc	= (WNDPROC)WndProc;
	wcex.cbClsExtra		= 0;
	wcex.cbWndExtra		= 0;
	wcex.hInstance		= hInstance;
	wcex.hIcon			= LoadIcon(NULL, IDI_APPLICATION);;
	wcex.hCursor		= LoadCursor(NULL, IDC_ARROW);
	wcex.hbrBackground	= NULL;//(HBRUSH)(COLOR_WINDOW+1);
	wcex.lpszMenuName	= NULL;
	wcex.lpszClassName	= "Novablaze";
	wcex.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
	if(!RegisterClassEx(&wcex)) return 0;

	wndHandle = CreateWindow("Novablaze", 
							 "Novablaze", 
							 WS_OVERLAPPEDWINDOW | WS_VISIBLE |
							 WS_SYSMENU |WS_CLIPCHILDREN |
							 WS_CLIPSIBLINGS,
							 CW_USEDEFAULT, 
							 CW_USEDEFAULT, 
							 640, 
							 480, 
							 NULL, 
							 NULL, 
							 hInstance, 
							 NULL);
   if (!wndHandle)
      return false;
   
   ShowWindow(wndHandle, SW_SHOW);
   UpdateWindow(wndHandle);

   return true;
}

/**********************************************************************
* WndProc - the window procedure
**********************************************************************/
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	switch (message) 
	{
		case WM_DESTROY:
			PostQuitMessage(0);
			break;
	}
	return DefWindowProc(hWnd, message, wParam, lParam);
}
